package com.google.code.gestion_de_conferences;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.code.gestion_de_conferences.AABDatabaseManager;

 
public class ForgotPasswordActivity extends Activity {
	
	AABDatabaseManager db;
	Button submit_btn;
	EditText reg_username,reg_password,reg_confirm_password;
	TextView reg_username_error ,reg_password_error ,reg_confirm_password_error;
	

	/**
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	 * @param Bundle
	 */
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
		
		super.onCreate(savedInstanceState);
		// Set View to register.xml
        setContentView(R.layout.forgot_password);
       
        db = new AABDatabaseManager(this);
        
        submit_btn = (Button) findViewById(R.id.btn_submit);
        reg_username = (EditText) findViewById(R.id.reg_username);
        reg_password = (EditText) findViewById(R.id.reg_password);
        reg_confirm_password = (EditText) findViewById(R.id.reg_confirm_password);
       
        reg_confirm_password_error = (TextView) findViewById(R.id.reg_confirm_password_error);
        reg_password_error = (TextView) findViewById(R.id.reg_password_error);
        reg_username_error = (TextView) findViewById(R.id.reg_username_error);
        
        
        submit_btn.setOnClickListener
    	(
    		new View.OnClickListener()
	    	{
				@Override public void onClick(View v) 
				{
				   	String username = reg_username.getText().toString();
			    	String pass = reg_password.getText().toString();
			    	String pass2 = reg_confirm_password.getText().toString();
			    	changePassword( username,  pass,  pass2);
				}
			}
    	);     
    }
	
		 
	/**
	 * This function validates the data sent on forgot password and display the related error message
	 * 
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	 *
	 * @param username
	 * @param pass
	 * @param pass2
	 * 
	 * @return Boolean
	 */
	private Boolean validate( String username, String pass, String pass2)
    {
        //first clear all error fields 
        clearErrors();
      
        Boolean error = false;
       
       
        User u=new User();
		u.setEmail(username);
       
        //check if there is users that have the same email
        ArrayList<String> rowArray = db.getRowAsArrayByUser(u);
        
       if(rowArray.size() == 0 || (rowArray.size()>0 && !rowArray.get(2).equals (username)))
       {
           error = true;
           reg_username_error.setText("Incorrect username!");      
       }
       if (pass.length()<6)
       {
            error = true;
            reg_password_error.setText( "Password'length is minimum 6!");
        }
       
        if(!pass.equals(pass2))
        {
            error =true;
            reg_confirm_password_error.setText( "Passwords don't match!");
        }

        return error;
    }
	
/**
	 * This function register a new Admin account into the DB
	 *
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	 * 
	 * @param username
	 * @param pass
	 * @param pass2
	 */
 private void changePassword( String username, String pass, String pass2 )
 {
	    ArrayList<ArrayList<Object>> result = new ArrayList<ArrayList<Object>>();
			
		//get the admin user object from the db
		result = db.getRowAsArrayByPosition(6,"User", 1+"");
			 
		//get the admin data
		ArrayList<Object> dbUser = result.get(0);			
				 	
    	Boolean error = validate( username,  pass,  pass2);
    	
    	if(!error)
    	{
    		try
    		{    			
    			User u=new User();
    			
				u.setID(Integer.parseInt(dbUser.get(0).toString()));
    			u.setFullName(dbUser.get(1).toString());
    			u.setEmail(dbUser.get(2).toString());
    			u.setCompany(dbUser.get(3).toString());
    			u.setIsAdmin("1");
    			u.setPassword(pass);
    			u.setPhoneNumber(dbUser.get(4).toString());  			
    			
    			//update row
    			db.updateRow (u);
    				
    			//success msg
    			Toast.makeText(this, "Your profile is successfully edited", Toast.LENGTH_LONG).show();
    				
    			//redirect to rooms page
    			Intent i = new Intent(getApplicationContext(), LoginActivity.class);
        	    startActivity(i);
  		         
    		}		
      		catch (Exception e)
        	{
        		Log.e("Update Error", e.toString());
        		e.printStackTrace();
        	}
    	}
    	
    }	
 
 
 /**
	 * This function clears all errors in register page
	 * @author Hyame Alameddine <halameddine@gmail.com> 
	 */
	private void clearErrors()
	{
        reg_username_error.setText("");
        reg_password_error.setText( "");        
        reg_confirm_password_error.setText( "");
    }
	
	/**
	 * This function create the menu options 
	 * @author Hyame Alameddine <halameddine@gmail.com> 
	 * @param menu
	 */
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.no_login_menu, menu);              
        return true;
    }
    
    /**
	   * This function handles clicking any of the menu's items
	   * 
	   * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	   * @param menu
	   */
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item) 
	  {	  
		  switch (item.getItemId()) 
		  {
	          case R.id.help:     
	                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	                alertDialog.setTitle("Help!");
	                alertDialog.setMessage("Gestion de conference is an application helps you manage reservations, add rooms ,add users and reserve rooms to organize conferences !");
	                alertDialog.setIcon(R.drawable.icon);
	                alertDialog.show();
	                break;
	          case R.id.developers: 
	            	Toast.makeText(this, "Rayan,Hyam,Mostafa!", Toast.LENGTH_LONG).show();
	                break;
	            
	        }
	        return true;
	    }
    
}


